package netWork;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class TcpFileUploadClient {
    /**
     * 客户端
     * @param args
     */
    public static void main(String[] args) throws IOException {
        System.out.println("客户端启动");

        File file = new File("C:\\Users\\86186\\Pictures\\Saved Pictures\\高数\\第一章节函数.png");
        try ( Socket socket = new Socket("127.0.0.1",9999);
              BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
             Scanner scannerInput = new Scanner(socket.getInputStream());
             BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             ){
            //从硬盘中读取图片转换成字节流
            int n = 0;
            //将图片读取
            byte[]b = new byte[4096];
            while ((n = bis.read(b))!= -1){
                baos.write(b,0,n);
            }
            byte[] array = baos.toByteArray();
            //把图片写入服务器
            bos.write(array);
            socket.shutdownOutput();//写入数据的结束
            //读取服务器返回的字符串
            String response =  scannerInput.next();
            System.out.println(response);

        }
    }
}
